﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace library
{
    public partial class purchase_information : Form
    {
        public MySqlConnection conn = new DBclass().getConn();
        public purchase_information()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            conn.Open();
            string sql = "select ISBN,uname as 推荐人,pur_time as 荐购日期,bname as 书名,author as 作者,press as 出版社,pub_time as 出版时间,rec_reason as 推荐理由,bought_status as 荐购状态,note as 处理备注,bought_status as 待购状态 from pur_info where bought_status='订购中'";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataAdapter sd = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            sd.Fill(ds, "pur_info");
            dataGridView3.DataSource = ds;
            dataGridView3.DataMember = "pur_info";
            conn.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            /*conn.Open();
            if (dataGridView3.Rows.Count > 0)
            {
                int j = 0;
                for (int i = 0; i < dataGridView3.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell checkCel1 = (DataGridViewCheckBoxCell)dataGridView3.Rows[i].Cells[0];
                    Boolean flag = Convert.ToBoolean(checkCel1.Value);
                    if (flag)
                    {
                        string ISBN = dataGridView3.Rows[i].Cells[1].Value.ToString();
                        string sql = "update pur_info set pur_status='1',note='已通过',bought_status='1' where ISBN=@ISBN";
                        MySqlCommand cmd = new MySqlCommand(sql, conn);
                        cmd.Parameters.Add("@ISBN", MySqlDbType.VarChar).Value = ISBN;
                        j += cmd.ExecuteNonQuery();
                        dataGridView3.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                    }
                }
                if (j > 0)
                { 
                    MessageBox.Show("修改成功");
                }
               conn.Close();}*/
            if (dataGridView3.Rows.Count > 0)
            {
                dataGridView3.EndEdit();//停止编辑状态
                foreach (DataGridViewRow row in dataGridView3.Rows)
                {
                    row.Cells[0].Value = true;
                    //row.Cells[0].Value = !(true.Equals(row.Cells[0].Value));
                }
            }
            conn.Open();
            if (dataGridView3.Rows.Count > 0)
            {
                int j = 0;
                for (int i = 0; i < dataGridView3.Rows.Count; i++)
                {
                    string ISBN = dataGridView3.Rows[i].Cells[1].Value.ToString();
                    MessageBox.Show(ISBN);
                    string sql = "update pur_info set pur_status='已通过',note='您的荐购信息已通过，图书正在订购中',bought_status='订购中' where ISBN=@ISBN";
                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    cmd.Parameters.Add("@ISBN", MySqlDbType.VarChar).Value = ISBN;
                    //j += cmd.ExecuteNonQuery();
                    //dataGridView3.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                    int num = cmd.ExecuteNonQuery();
                    if (num > 0)
                    {
                        MessageBox.Show("已通过");
                    }
                }
                if (j > 0)
                {
                    MessageBox.Show("修改成功");
                }
            }
            conn.Close();
            this.Close();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (dataGridView3.Rows.Count > 0)
            {
                dataGridView3.EndEdit();//停止编辑状态
                foreach (DataGridViewRow row in dataGridView3.Rows)
                {
                    row.Cells[0].Value = true;
                }
            }
            conn.Open();
            if (dataGridView3.Rows.Count > 0)
            {
                int j = 0;
                for (int i = 0; i < dataGridView3.Rows.Count; i++)
                {
                    string ISBN = dataGridView3.Rows[i].Cells[1].Value.ToString();
                    MessageBox.Show(ISBN);
                    string sql = "update pur_info set pur_status='未通过',note='很抱歉您的荐购申请未通过',bought_status='未通过' where ISBN=@ISBN";
                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    cmd.Parameters.Add("@ISBN", MySqlDbType.VarChar).Value = ISBN;
                    j += cmd.ExecuteNonQuery();
                    dataGridView3.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                    int num = cmd.ExecuteNonQuery();
                    if (num > 0)
                    {
                        MessageBox.Show("不通过");
                    }
                }
                if (j > 0)
                {
                    MessageBox.Show("修改成功");
                }
            }
            conn.Close();
            this.Close();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (dataGridView3.Rows.Count > 0)
            {
                dataGridView3.EndEdit();//停止编辑状态
                foreach (DataGridViewRow row in dataGridView3.Rows)
                {
                    row.Cells[0].Value = true;
                }
            }
            conn.Open();
            if (dataGridView3.Rows.Count > 0)
            {
                int j = 0;
                for (int i = 0; i < dataGridView3.Rows.Count; i++)
                {
                    string ISBN = dataGridView3.Rows[i].Cells[1].Value.ToString();
                    MessageBox.Show(ISBN);
                    string sql = "update pur_info set note='您所荐购的图书已添加至图书馆收藏',bought_status='订购完成' where bought_status='订购中'";
                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    cmd.Parameters.Add("@ISBN", MySqlDbType.VarChar).Value = ISBN;
                    j += cmd.ExecuteNonQuery();
                    dataGridView3.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                    int num = cmd.ExecuteNonQuery();
                    if (num > 0)
                    {
                        MessageBox.Show("通过");
                    }
                }
                if (j > 0)
                {
                    MessageBox.Show("加入图书成功");
                }
            }
            conn.Close();
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            
        }

        private void purchase_information_Load(object sender, EventArgs e)
        {
            this.dataGridView3.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            conn.Open();
            string sql = "select ISBN,uname as 推荐人,pur_time as 荐购日期,bname as 书名,author as 作者,press as 出版社,pub_time as 出版时间,rec_reason as 推荐理由,pur_status as 荐购状态,note as 处理备注,bought_status as 待购状态 from pur_info where pur_status='待审核' and bought_status='待审核'";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataAdapter sd = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            sd.Fill(ds, "pur_info");
            dataGridView3.DataSource = ds;
            dataGridView3.DataMember = "pur_info";
            conn.Close();
        }


        private void button1_Click_1(object sender, EventArgs e)
        {
            conn.Open();
            string sql = "select ISBN,uname as 推荐人,pur_time as 荐购日期,bname as 书名,author as 作者,press as 出版社,pub_time as 出版时间,rec_reason as 推荐理由,bought_status as 荐购状态,note as 处理备注,bought_status as 待购状态 from pur_info";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataAdapter sd = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            sd.Fill(ds, "pur_info");
            dataGridView3.DataSource = ds;
            dataGridView3.DataMember = "pur_info";
            conn.Close();
        }
    }
}
